<!--
 * @Author: FengTeng
 * @Date: 2021-02-12 08:48:10
 * @LastEditTime: 2021-03-03 15:21:12
 * @LastEditors: FengTeng
 * @Description: Description
 * @FilePath: \spa-manage\src\App.vue
-->
<template>
  <router-view />
</template>

<script>
import utils from "@/utils/init.permissions.js";
import { SET_PEIMISSION_INFO, authPermissions } from "@/store/auth.permission.js";
import { SET_USER_INFO, authInfo } from "@/store/userinfo.js";
import { mapMutations } from "vuex";
export default {
  created() {
    this.initPermissions();
    if (sessionStorage.getItem("state")) {
      let permissions = JSON.parse(sessionStorage.getItem("state"));
      this.setPermissionInfo(permissions);
    }
    if (sessionStorage.getItem("userInfo")) {
      let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      this.setUserInfo(userInfo);
    }
    // 在页面刷新时将store保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      console.log(authPermissions.state.permissions);
      sessionStorage.setItem("state", JSON.stringify(authPermissions.state.permissions));
      sessionStorage.setItem("userInfo", JSON.stringify(authInfo.state.userinfo));
    });
  },
  methods: {
    ...mapMutations("permissionModule", {
      setPermissionInfo: SET_PEIMISSION_INFO
    }),
    ...mapMutations("authInfoModule", {
      setUserInfo: SET_USER_INFO
    }),
    initPermissions() {
      let permissions = utils.initPermissions();
      this.setPermissionInfo(permissions);
      console.log(authPermissions);
    }
  }
};
</script>

<style lang="scss" src="./index.scss"></style>
